#!/bin/bash

# 检查是否以root运行
#if [ $USER == "root" ]; then
#    echo $USER
#    echo "这个脚本不能以root的方式运行，建议单独创建go-caipu用户" >&2
#    exit 1
#fi
# 创建用户和组
#if ! id -u go-caipu >/dev/null 2>&1; then
#    echo "Creating go-caipu user..."
#    useradd -r -s /bin/false go-caipu
#fi

# 创建程序目录,检查目录是否存在
if [ ! -d "/opt/go-caipu" ]; then
    mkdir -p /opt/go-caipu
    sudo chown go-caipu:go-caipu /opt/go-caipu
    sudo chmod 755 /opt/go-caipu
fi

# 移动程序到 /opt/go-caipu 目录
sudo rm -rf /opt/go-caipu/go-caipu
mv /tmp/go-caipu /opt/go-caipu/go-caipu
sudo chown go-caipu:go-caipu /opt/go-caipu/go-caipu
sudo chmod 750 /opt/go-caipu/go-caipu

#关闭当前程序正在运行的程序
if ps aux | grep -v grep | grep go-caipu > /dev/null; then
    echo "Stopping go-caipu..."
    sudo pkill -f '/go-caipu'
fi

echo '启动 go-caipu 服务'
# 等待3s
sleep 3
cd /opt/go-caipu/
sudo nohup ./go-caipu > go-caipu.log 2>&1 &
echo '启动 go-caipu 服务完成'

echo '显示正在运行的服务 '
ps aux | grep go-caipu
echo 'go-caipu 启动完成‘



